<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

namespace app\common\model;

use PDO;
use Qiniu\Config;
use think\Model;
use think\Db;
class OrderCheck extends Model
{
    public $id=null;
    //订单状态是否异常(影响分配调配单)
    public $order_status_change=false;
    //旅客是否异常(影响分配调配单)
    public $traveler_change=false;
    //订单信息是否一致（不影响调配单）
    public $order_change=false;
    //旧订单信息
    public $old_order_info=null;
    //新订单信息
    public $nwe_order_info=null;
    public $oid=null;
    public $old_traveler=null;
    public $new_traveler=null;
    public $dispatch=null;




    //注意，必须在修改前检测
    public function check($nwe_order_info,$old_order_info)
    {
        $this->nwe_order_info=$nwe_order_info;
        $this->new_traveler=$nwe_order_info['travelers'];
        $this->oid=$old_order_info['id'];
        if(!$this->oid){
            exception('新订单没有订单号参数');
        }
        $this->old_order_info=$old_order_info;
        $this->old_traveler=$old_order_info['travelers'];
        $dispatch_order=Db::name('dispatch_order')->where("oid",$this->oid)->find();


        if($dispatch_order){
            $this->dispatch=Db::name("dispatch")->where('id',$dispatch_order['dispatch_id'])->find();
        }
        $this->order_status_change();
//        $this->add_order_check();
//        $this->add_exceptional_order();
        return $this;
    }
    public function order_status_change(){
        if($this->dispatch['is_dispatch_traveller']){
            if($this->traveler_change()){
                $this->order_status_change=true;
            }
        }
        return $this->order_status_change;
    }
    //检查旅客数量是否异常
    public function traveler_num(){
        //只要是退款就异常

            if($this->nwe_order_info['isrefund']&&!$this->old_order_info['isrefund']){
                $this->traveler_change=true;
                return true;
            }
            //不是平台订单无法检查旅客数量，默认一致
            if($this->nwe_order_info['platform']!="manually"){
                return $this->traveler_change;
            }
            $old_traveler_num=count($this->old_traveler);
            $new_traveler_num=count($this->new_traveler);
            if($old_traveler_num!=$new_traveler_num){
                $this->traveler_change=true;
                return true;

            }else{
                $this->traveler_change=false;
                return false;
            }
    }
    //检查证件是否一致
    public function traveler_change()
    {
        if(!$this->traveler_num()){
                //转换以证件为数组key
                $new_traveler=[];
          		if(!$this->new_traveler){
                	return $this->traveler_change;
                }
                foreach ($this->new_traveler as $val){
                    $new_traveler[$val['id_card']]=$val;
                }

                $old_traveler=[];
                foreach ($this->old_traveler as $val){
                    $old_traveler[$val['id_card']]=$val;
                }

                //对比旅客证件
                foreach ($new_traveler as $key=>$val){
                        if(!isset($old_traveler[$key])){
                            $this->traveler_change=true;
                        }
                }


        }
        return $this->traveler_change;
    }

    //检查订单数据是否一致 返回不一致的信息
    public function check_order_info(){
        $check['order_id']=$this->old_traveler['order_id']==$this->new_traveler['order_id'];
        $check['order_status']=$this->old_traveler['order_status']==$this->new_traveler['order_status'];
        $check['isrefund']=$this->old_traveler['isrefund']==$this->new_traveler['isrefund'];
        $check['refund_status']=$this->old_traveler['refund_status']==$this->new_traveler['refund_status'];
        $check['route_id']=$this->old_traveler['route_id']==$this->new_traveler['route_id'];
        $check['money']=$this->old_traveler['money']==$this->new_traveler['money'];
        $check['title']=$this->old_traveler['title']==$this->new_traveler['title'];
        $check['refund_money']=$this->old_traveler['refund_money']==$this->new_traveler['refund_money'];
        $check['platform']=$this->old_traveler['platform']==$this->new_traveler['platform'];
        $return=[];
        foreach ($check as $key=>$val){
            if(!$val){
                $return[$key]=$val;
            }
        }
        $this->order_change=$return;
        return $return;
    }

    //检查开始和结束日期
    public function check_date(){
        $check['start_time']=$this->old_traveler['start_time']==$this->new_traveler['start_time'];
        $check['end_time']=$this->old_traveler['end_time']==$this->new_traveler['end_time'];
        $return=[];
        foreach ($check as $key=>$val){
            if(!$val){
                $return[$key]=$val;
            }
        }
        return $return;
    }
    public function add_order_check()
    {
        $order_check['oid']=$this->oid;
        $order_check['order_status_change']=$this->order_status_change;
        $order_check['traveler_change']=$this->traveler_change;
        $order_check['order_change']=$this->order_change;
        $order_check['old_order_info']=json_encode($this->old_order_info);
        $order_check['nwe_order_info']=json_encode($this->nwe_order_info);
        $order_check['create_time']=NOW_TIME;
        $order_check['update_time']=NOW_TIME;
        $id=Db::name('order_check')->insertGetId($order_check);
        $order_check['id'];
        $this->id=$id;
        return $order_check;
    }
    public function add_exceptional_order()
    {
        if($this->order_status_change){
             $exceptional_order['oid']=$this->oid;
             $exceptional_order['order_check_id']=$this->id;
            $exceptional_order['exception_info']=json_encode(array_merge($this->check_order_info(),$this->check_date()));
            $exceptional_order['solve']=false;
            $exceptional_order['traveler_change']=$this->traveler_change;
            if($this->nwe_order_info['isrefund']&&!$this->old_order_info['isrefund']){
                $exceptional_order['is_refund']=true;
            }
            $id=Db::name('exceptional_order')->insertGetId($exceptional_order);
            $exceptional_order['id']=$id;
        }

        return $exceptional_order;
    }
    public function mfw_transform_order($mfw_order)
    {
//        $db_order=Db::name('order')->where('order_id',$mfw_order['order_id'])->find();
        $order['order_id']=$mfw_order['order_id'];
        logs('mfw_order20190314',$mfw_order);
        $order['order_status']=config("mfw_order_status")[$mfw_order['order_status']];
        $order['isrefund']=$mfw_order['refund_status']?true:false;
        $order['refund_status']=config("mfw_refund_status")[$mfw_order['refund_status']];
        $order['route_id']=Db::name('route_product')
            ->where("find_in_set('{$mfw_order['sales_id']}',`product_id`)" )
            ->where('route_category_id','=',1)
            ->value('route_id');
        $order['money']=$mfw_order['payment_fee'];
        $order['title']=$mfw_order['sales_name'];
        $order['platform']="mfw";
        $order['start_time']=$mfw_order['go_date'];
        $mfw_booking_people=Db::name("mfw_booking_people")->where('mfw_order_id',$mfw_order['order_id'])->find();
        $order['booking_people_name']=$mfw_booking_people['name'];
        $order['booking_people_email']=$mfw_booking_people['email'];
        $order['booking_people_phone']=$mfw_booking_people['phone'];
        $order['booking_people_wechat']=$mfw_booking_people['wechat'];
        $order['booking_people_remark']=$mfw_booking_people['remark'];
        $order['booking_people_phone_area']=$mfw_booking_people['phone_area'];
        $insert['all_refund_flag'] = $mfw_order['all_refund_flag'];
        $order['sales_id']=$mfw_order['sales_id'];
        $order['ctime']=$mfw_order['ctime'];
        $order['sku_id']=$mfw_order['sku_id'];
        $order['uid'] = Db::name('mfw_booking_people')->where('mfw_order_id',$mfw_order['order_id'])->value('uid');
        $order['sales_name'] = $mfw_order['sales_name'];
        $order['sku_name'] = $mfw_order['sku_name'];
        $order['total_price'] = $mfw_order['total_price'];
        $order['payment_fee'] = $mfw_order['payment_fee'];
        $str = json_decode($mfw_order['promotion_detail'],JSON_OBJECT_AS_ARRAY);
        $order['reduce_mfw'] = $str['reduce_mfw'];
        $order['reduce_ota'] = $str['reduce_ota'];
        $order['ota_sales_name'] = $str['ota_sales_name'];

        $order['days']=$mfw_order['ota_sales_name'];

        if($order['sku_id']){
            $mfw_api=new \app\common\model\MfwApi();
            $mfw_api->get_sku_info($order['sku_id']);
        }
        return $order;
    }
    public static function task($order_id=null)
    {
        logs('mfw_order', $order_id);
        if(!$order_id){
            $mfw_order=Db::name('mfw_order')->where('ischeck',"<>",'1')->order('create_time asc')->find();
        }else{
            $mfw_order=Db::name('mfw_order')->where('order_id',$order_id)->order('create_time asc')->find();
        }
        logs('mfw_order', $mfw_order);
//        dump($mfw_order);
        if($mfw_order){
            $old_order=Db::name('order')->where('order_id',$mfw_order['order_id'])->find();
            $m_order_check=new \app\common\model\OrderCheck();
            $new_order=$m_order_check->mfw_transform_order($mfw_order);

            if($old_order){
                $mfw_order['id']=$old_order['id'];
                $m_order_check->check($new_order,$old_order);
                $m_order_check->add_order_check();
                $m_order_check->add_exceptional_order();
//                if($new_order['is_refund']){ //是否退款
//                    //如果订单退款，则将订单设置为异常状态，同时将该订单下的订单旅客设置为异常状态
//                    $new_order['dispose_status'] = 'error';
//                    Db::name('order_traveler')->where('oid',$new_order['order_id'])->update(array('is_error'=>1));
//                }
                Db::name('order')->where('order_id',$new_order['order_id'])->update($new_order);
            }else{
                Db::name('order')->insert($new_order);
            }

            Db::name('mfw_order')->where('order_id',$mfw_order['order_id'])->update(array('ischeck'=>true));
        }
    }
}
